﻿using System;
using System.Globalization;
using System.Web.UI;
using VipCo.Entities;
using VipCo.Data;

public partial class Admin_SanPham_CNSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpChuyenMuc + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            BindCatalog.BindNhomSanPham(cboNhomSanPham);
            BindCatalog.BindThanhPho(cboThanhPho);

            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"];
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }

            TList<QuanHuyen> listCatalogs = DataRepository.QuanHuyenProvider.Find("IdThanhPho=" + cboThanhPho.SelectedValue);
            cboQuanHuyen.DataSource = listCatalogs;
            cboQuanHuyen.DataTextField = "TenQuanHuyen";
            cboQuanHuyen.DataValueField = "Id";
            cboQuanHuyen.DataBind();
            HiddenField1.Value = cboQuanHuyen.SelectedValue;
        }

        HiddenField1.Value = cboQuanHuyen.SelectedValue;
    }

    protected void BindData(int idCategories)
    {
        var entDiaDiem = DataRepository.DiaDiemProvider.GetById(idCategories);
        cboNhomSanPham.SelectedValue = entDiaDiem.IdChuyenMuc.ToString(CultureInfo.InvariantCulture);
        cboThanhPho.SelectedValue = entDiaDiem.IdThanhPho.ToString(CultureInfo.InvariantCulture);
        cboQuanHuyen.SelectedValue = entDiaDiem.IdQuanHuyen.ToString(CultureInfo.InvariantCulture);
        txtTieuDeTin.Text = entDiaDiem.TenDiaDiem;
        H_AnhDaiDien.Value = entDiaDiem.AnhDaiDien;
        imgViewer.ImageUrl = entDiaDiem.AnhDaiDien.Length > 0 ? Common.sPathUpChuyenMuc + entDiaDiem.AnhDaiDien : "~/Images/no_image.jpg";
        txtTomTat.Text = Server.HtmlDecode(entDiaDiem.TomTat);
        txtContent.Text = Server.HtmlDecode(entDiaDiem.NoiDung);
        txtDiaChi.Text = entDiaDiem.DiaChi;
        txtHoaDon.Text = entDiaDiem.HoaDon;
        txtPhanTram.Text = entDiaDiem.PhanTram.ToString(CultureInfo.InvariantCulture);
        txtNgayBatDau.Text = entDiaDiem.NgayBatDau.ToString("dd/MM/yyyy");
        txtNgayKetThuc.Text = entDiaDiem.NgayKetThuc.ToString("dd/MM/yyyy");
        txtNoiBat.Text = Server.HtmlDecode(entDiaDiem.DiemNoiBat);
        txtSuDung.Text = Server.HtmlDecode(entDiaDiem.DieuKienSuDung);
        tbUutien.Text = entDiaDiem.UuTien.ToString();
        H_TacGia.Value = entDiaDiem.TacGia;
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            DiaDiem entDiadiem = new DiaDiem();
            entDiadiem.Id = Convert.ToInt32(H_CatalogId.Value);
            entDiadiem.IdChuyenMuc = Convert.ToInt32(cboNhomSanPham.SelectedValue);
            entDiadiem.IdThanhPho = Convert.ToInt32(cboThanhPho.SelectedValue);
            entDiadiem.IdQuanHuyen = Convert.ToInt32(cboQuanHuyen.SelectedValue);
            entDiadiem.TenDiaDiem = Common.StandardString(txtTieuDeTin.Text);
            entDiadiem.TomTat = Common.StandardString(txtTomTat.Text);
            entDiadiem.DiaChi = Common.StandardString(txtDiaChi.Text);
            entDiadiem.HoaDon = Common.StandardString(txtHoaDon.Text);
            entDiadiem.TrangThai = chkTinNong.Checked;
            entDiadiem.PhanTram = Convert.ToInt32(txtPhanTram.Text);
            entDiadiem.DieuKienSuDung = Server.HtmlEncode(txtSuDung.Text);
            entDiadiem.DiemNoiBat = Server.HtmlEncode(txtNoiBat.Text);
            entDiadiem.NoiDung = Server.HtmlEncode(txtContent.Text);
            entDiadiem.TacGia = Common.StandardString(H_TacGia.Value);
            entDiadiem.NgayCapNhat = Common.GetServerDateTime();
            entDiadiem.UuTien = Convert.ToInt32(tbUutien.Text);            

            if (H_AnhDaiDien.Value.Length == 0)
            {
                entDiadiem.AnhDaiDien = "~/Images/no_image.jpg";
            }
            else
            {
                entDiadiem.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
            }
            if (H_TacGia.Value == "")
            {
                entDiadiem.TacGia = Common.GetKeyUserInfo("TenTruyCap");
            }
            else
            {
                entDiadiem.TacGia = H_TacGia.Value;
            }

            DateTime dt1 = DateTime.ParseExact(Common.StandardString(txtNgayBatDau.Text), "dd/MM/yyyy", null);
            DateTime dt2 = DateTime.ParseExact(Common.StandardString(txtNgayKetThuc.Text), "dd/MM/yyyy", null);

            if (dt1 > dt2)
            {
                lblSoSanh.Text = "Ngày kết thúc không thể nhỏ hơn ngày bắt đầu!";
                return;
            }

            entDiadiem.NgayBatDau = dt1;
            entDiadiem.NgayKetThuc = dt2;

            if (entDiadiem.Id == 0)
            {
                updateStatus = DataRepository.DiaDiemProvider.Insert(entDiadiem);
                cmdNhapTiep_Click(sender, e);
            }
            else
            {
                entDiadiem.EntityState = EntityState.Changed;
                entDiadiem.OriginalId = entDiadiem.Id;
                updateStatus = DataRepository.DiaDiemProvider.Update(entDiadiem);                
            }
            if (!updateStatus)
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TINTUC_CAPNHAT_THANHCONG, true);
                H_CatalogId.Value = entDiadiem.Id.ToString();
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("CNSanPham.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        txtTieuDeTin.Text = "";
        txtTomTat.Text = "";        
        cboThanhPho.SelectedValue = "0";
        chkTinNong.Checked = true;
        txtContent.Text = "";
        txtDiaChi.Text = "";
        txtHoaDon.Text = "";
        txtPhanTram.Text = "0";
        txtNoiBat.Text = "";
        txtSuDung.Text = "";
        txtNgayBatDau.Text = "";
        txtNgayKetThuc.Text = "";
        cboNhomSanPham.SelectedValue = "0";
        H_TacGia.Value = "";
    }

    protected void SelectedindexchangedcboNhomTin(object sender, EventArgs e)
    {
        TList<QuanHuyen> listCatalogs = DataRepository.QuanHuyenProvider.Find("IdThanhPho=" + cboThanhPho.SelectedValue);
        cboQuanHuyen.DataSource = listCatalogs;
        cboQuanHuyen.DataTextField = "TenQuanHuyen";
        cboQuanHuyen.DataValueField = "Id";
        cboQuanHuyen.DataBind();
    }

    protected void SelectedindexchangedcboSubTin(object sender, EventArgs e)
    {
        HiddenField1.Value = cboQuanHuyen.SelectedValue;
    }
}